package com.example.testdemo.imaginary;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.testdemo.R;

/**
 * Created by MrYan on 2018/8/15.
 * 虚线View Item 指示器
 */

public class ImaginaryView extends View {
    private int lineColor;
    private Paint pointPaint;
    private Paint linePaint;
    private Path topPath;
    private Path bottomPath;
    private boolean top;
    private boolean bottom;

    public ImaginaryView(Context context) {
        super(context);
    }

    public ImaginaryView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        lineColor = getResources().getColor(R.color.cardview_dark_background);
        pointPaint = new Paint();
        pointPaint.setAntiAlias(true);
        pointPaint.setColor(lineColor);
        linePaint = new Paint();
        linePaint.setColor(lineColor);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(2);
        linePaint.setPathEffect(new DashPathEffect(new float[]{8, 2}, 0));
        top = false;
        bottom = false;
    }

    public ImaginaryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        topPath = new Path();
        topPath.moveTo(getWidth() / 2, 0);
        topPath.lineTo(getWidth() / 2, getHeight() / 2 - 10);
        bottomPath = new Path();
        bottomPath.moveTo(getWidth() / 2, getHeight() / 2 + 10);
        bottomPath.lineTo(getWidth() / 2, getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!top) {
            canvas.drawPath(topPath, linePaint);
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, 5, pointPaint);
        }
        if (!bottom) {
            canvas.drawPath(bottomPath, linePaint);
        }
    }

    public void setTop(boolean top) {
        this.top = top;
        this.bottom = !top;
        postInvalidate();
    }

    public void setBottom(boolean bottom) {
        this.bottom = bottom;
        this.top = !bottom;
        postInvalidate();
    }
}
